<?php

/**
 * MyHammer IndexedMemcache
 *
 * LICENSE
 *
 * This source file is subject to the GNU General Public License Version 3
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.myhammer.de/opensource/license/gpl.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to opensource@myhammer.de so we can send you a copy immediately.
 *
 * @category   MyHammer
 * @package    MyHammer_IndexedMemcache
 * @subpackage Memcache
 * @license    http://www.myhammer.de/opensource/license/gpl.txt GNU General Public License Version 3
 * @version    1.0
 * @author     Steven Berg <berg@myhammer.de>
 */

class cMemCacheFiles {

    /**
     * save MemCacheModel into MemCache
     * - MemCacheModel convert to jSonString or PhpObject
     * - create and save index
     *
     * @param cMemCacheModel $oMemCacheModel
     */
    public static function saveObject(cMemCacheModel $oMemCacheModel) {
        cProfiler::startPoint('saveObject');

        $iLinkId = self::createLinkIdToGuId();
        $sGuId = $oMemCacheModel->getGuId();

        $oMemCacheModel->setLinkId($iLinkId);
        $sDataContainer = self::createObjectContainer($oMemCacheModel);

        // save MemCacheObject to GuId
        cMemCache::write($sGuId, $sDataContainer);

        // save MemCacheLink to GuId
        cMemCache::write($iLinkId, $sGuId);

        // save Index for Object
        cMemCacheIndex::saveIndex($oMemCacheModel);

        cProfiler::endPoint('saveObject');

        return $iLinkId;
    }


    /**
     * convert MemCacheOBject to DataContainer
     *
     * @param cMemCacheModel $oMemCacheModel
     * @return string - json
     */
    public static function createObjectContainer(cMemCacheModel $oMemCacheModel) {
        return cMemCacheDataContainer::encode($oMemCacheModel);
    }

    /**
     * convert DataContainer to MemCacheObject
     *
     * @param string $sDataContainer
     * @return cMemCacheModel
     */
    public static function createObjectFromContainer($sDataContainer) {
        return cMemCacheDataContainer::decode($sDataContainer);
    }

    /**
     * create next LinkId
     *
     * @return int
     */
    private static function createLinkIdToGuId() {
        $sLinkIdKey = cConfigMemCache::IDENTIFIER_LINK_ID . '|' . cConfigMemCache::IDENTIFIER_CACHE_VERSION;

        $iLinkId = cMemCache::read($sLinkIdKey);
        if ($iLinkId === FALSE) {
            $iLinkId = 0;
        }
        $iLinkId++;
        cMemCache::write($sLinkIdKey, $iLinkId);

        return $iLinkId;
    }

    /**
     * get MemCacheModelObjects by LinkIds
     *
     * @param array $aLinkIds
     * @return array
     */
    public static function getObjectByLinkIds($aLinkIds) {
        cProfiler::startPoint('readObjectAndConvertToObjects');
        $aObjects = array();

        foreach ($aLinkIds as $iLinkId => $sNull) {
            $sGuId = cMemCache::read($iLinkId);
            $sDataContainer = cMemCache::read($sGuId);
            $aObjects[] = self::createObjectFromContainer($sDataContainer);
        }

        cProfiler::endPoint('readObjectAndConvertToObjects');
        return $aObjects;
    }

    /**
     * get Json by LinkIds
     *
     * @param array $aLinkIds
     * @return array
     */
    public static function getJsonByLinkIds($aLinkIds) {
        cProfiler::startPoint('readObjectAndConvertToJson');
        $aJson = array();

        foreach ($aLinkIds as $iLinkId => $sNull) {
            $sGuId = cMemCache::read($iLinkId);
            $aJson[] = cMemCache::read($sGuId);
        }

        cProfiler::endPoint('readObjectAndConvertToJson');
        return $aJson;
    }
}